import Axios from "axios";
import { ElMessage } from 'element-plus';
// import router from '@/router/index';
import store from '@/store/index';

// 请求地址
const https = process.env.NODE_ENV == 'production' ? '/' : '/api';
// headers请求头等
let axios = Axios.create({
    baseURL: https,
    headers: {
        'X-SessionId': sessionStorage.getItem('sessionId') || ''
    },
    timeout: 30000
});

// 每次请求拿最新token;
axios.interceptors.request.use(
    (config) => {
        config.headers['X-SessionId'] = sessionStorage.getItem('sessionId') || '';
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

// 添加响应器
axios.interceptors.response.use(
    res => resHandler(res.data),
    err => {
        console.log(err);
        ElMessage.error('服务报错.');
        return Promise.reject(err);
    });

// 过期状态码在此处拦截
function resHandler (res) {
    // 如果是登陆过期并且在注册或者登陆页，不弹窗提示过期
    // 登陆失效的码
    // const codeList = [507, 506, 10111, 10112, 10113, 10114, 10115,];
    // if (codeList.indexOf(res.data.code) != -1 && router.app._route.name !== 'login' && router.app._route.name !== 'register') {
    //     // 如果是登陆过期并且在注册或者登陆页，不弹窗提示过期
    //     Message.error('登陆已过期');
    //     setTimeout(() => {
    //         router.push({ path: '/login' });
    //         store.commit('deleteUserData', '');
    //     }, 2000);
    // }
    return Promise.resolve(res);
}

export const postAxios = (url, data, config = {}) => {
    return axios.post(`${url}`, data, {
        headers: { ...config }
    });
};


export const putAxios = (url, data) => {
    return axios.put(`${url}`, data);
};

export const getAxios = (url, data) => {
    return axios.get(`${url}`, { params: data });
};

export const deleteAxios = (url, data) => {
    return axios.delete(`${url}`, { params: data });
};